package StudyCode.summary.myfile;

import java.util.Arrays;
import java.util.BitSet;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ConvertByteArrayToBitSet {
	public static void main(String[] args) {
		byte[] bytes = new byte[]{34, 35, 36, 37, 37, 37, 67, 68, 69};
		System.out.println("字节数组:" + Arrays.toString(bytes) + "转为 BitSet:");
		System.out.println(fromByteArray(bytes));
	}

	private static BitSet fromByteArray(byte[] bytes) {
		BitSet bits = new BitSet();
		for (int i = 0; i < bytes.length * 8; i++) {
			if ((bytes[bytes.length - i / 8 - 1] & (1 << (i % 8))) > 0) {
				bits.set(i);
			}
		}
		return bits;
	}
}
